﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 
using U9API.Pub;
using System.Data;
using U9API.JsonModel;
using Newtonsoft.Json;
using System.Reflection;

namespace U9API.APIHelper
{
     

    public class RMA
    {
        public Int64 ID { get; set; }
        public string DocumentType { get; set; }
        public string DocNo { get; set; }
        public string BusinessDate { get; set; }
        public Int64 Seller { get; set; }
        public string Customer_Code { get; set; }
        public Int64 Customer_Customer { get; set; }
        public Int32 RMAType{ get; set; }
        public Int32 SrcDocType { get; set; }
        public string TC { get; set; }
        public string SrcDocNo { get; set; }
        public Int32 MergeAR { get; set; }
        public bool IsInit { get; set; }
        public Int32 Status{ get; set; } 
        public string SrcTaxTINo { get; set; }
        public string WarrantNo { get; set; }
        public decimal ApplyMoneyTC { get; set; }
        public List<RMALine> RMALines { get; set; }
        public string SellerName { get; set; }
        public Int64 SaleDept { get; set; }

        public string SaleDeptCode { get; set; }
        public string SellerCode { get; set; }
    }


    public class RMALine
    {
        public Int64 ID { get; set; }
        public int DocLineNo { get; set; }
        public Int64 ItemInfo_ItemID { get; set; }
        public string ItemInfo_ItemCode { get; set; }
        public string ItemInfo_ItemName { get; set; }
        public string ItemInfo_ItemSpecs { get; set; }
        public int Status { get; set; }
        public Int64 BillToSite_CustomerSite { get; set; }
        public Int64 PayerSite_CustomerSite { get; set; }
        public decimal OrderPrice { get; set; }
        public decimal ApplyQtyTU1 { get; set; }
        public string QCDesc { get; set; }
        public string TU { get; set; }
        public string PU { get; set; }
        public string DescFlexField_PrivateDescSeg1 { get; set; }
        public Int64 SrcSoDoc { get; set; }
        public string SrcSoDocNo { get; set; }
        public Int64 SrcDoc_EntityID { get; set; }
        public Int64 SrcDocLine_EntityID { get; set; }
        public string SrcDocNo { get; set; }
        public decimal ApplyMoneyTC { get; set; }
        
        
    }



    public class ShipReturnQueryHelper : BaseHelper<JsonModel.ApiQuery>
    { 
        public List<U9API.DBHelper.Model.Customer> Resdatas { get; set; }
        public string result; 


        private static RMA DataRowToHModel(DataRow row)
        {
            RMA model = new RMA();
            Type examType = typeof(RMA);
            BindingFlags flag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
            if (row != null)
            {
                DataColumnCollection dc = row.Table.Columns;
                for (int i = 0; i < dc.Count; i++)
                {
                    if (row[dc[i].ColumnName] != null && row[dc[i].ColumnName].ToString() != "")
                    {
                        PropertyInfo piInstance = examType.GetProperty(dc[i].ColumnName, flag);
                        if (piInstance != null)
                        {
                            piInstance.SetValue(model, row[dc[i].ColumnName], null);
                        }
                    }
                }
            }

            return model;
        }

        private static RMALine DataRowToBModel(DataRow row)
        {
            RMALine model = new RMALine();

            Type examType = typeof(RMALine);
            BindingFlags flag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
            if (row != null)
            {
                DataColumnCollection dc = row.Table.Columns;
                for (int i = 0; i < dc.Count; i++)
                {
                    if (row[dc[i].ColumnName] != null && row[dc[i].ColumnName].ToString() != "")
                    {
                        PropertyInfo piInstance = examType.GetProperty(dc[i].ColumnName, flag);
                        if (piInstance != null)
                        {
                            piInstance.SetValue(model, row[dc[i].ColumnName], null);
                        }
                    }
                }
            }
            return model;
        }

        protected override void Create(string U9constr, int operation, List<JsonModel.ApiQuery> models )
        {
            //Pub.WriteLog.WriteLine(DateTime.Now.ToShortDateString() + ": CustomerHelper");
            if (models.Count == 0)
            {
                throw new Exception("查询条件不允许为空!");
            }
            JsonModel.ApiQuery model = models[0];
            StringBuilder sql = new StringBuilder();
            sql.Append("select  A7.Code as SellerCode, A8.Code as SaleDeptCode, A.Seller, A.SaleDept, A.ID, A.DocNo, A4.NAME AS SellerName, A2.Code as DocumentType, A.Customer_Code, A.Customer_Customer, A.RMAType, Convert(varchar(10), BusinessDate ,121) as BusinessDate,  A3.NAME as TC, A.SrcDocType, A.SrcDocNo, A.MergeAR, A.IsInit , A.Status, A.SrcTaxTINo, A.WarrantNo, A.ApplyMoneyTC ");
            sql.Append(" from SM_RMA A ");
            sql.Append(" LEFT JOIN SM_RMADocType A2 ON A2.ID = A.DocumentType ");
            sql.Append(" LEFT JOIN CBO_Operators_Trl A4 ON A4.ID = A.Seller "); 
            sql.Append(" LEFT JOIN CBO_Operators A7 ON A7.ID = A.Seller ");
            sql.Append(" LEFT JOIN CBO_Department A8 on A8.ID = A.SaleDept");
            sql.Append(" LEFT JOIN Base_Currency_Trl A3 ON A3.ID = A.TC where 1=1 ");

            if (!string.IsNullOrEmpty(model.code))
            {
                sql.Append("and A.DocNo = '" + model.code + "'");
            }
            if (!string.IsNullOrEmpty(model.id))
            {
                sql.Append("and A.ID = '" + model.id + "'");
            }
            if (!string.IsNullOrEmpty(model.customer))
            {
                sql.Append("and A.Customer_Customer = '" + model.customer + "'");
            }
            if (!string.IsNullOrEmpty(model.startdate))
            {
                sql.Append(" and (A.CreatedOn >='" + model.startdate + "' or A.ModifiedOn >='" + model.startdate + "')");
            }
            if (!string.IsNullOrEmpty(model.enddate))
            {
                sql.Append(" and (A.CreatedOn <='" + model.enddate + "' or A.ModifiedOn <='" + model.enddate + "')");
            }
            DataSet ds = DbHelperSQL.Query(sql.ToString(), U9constr);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                List<RMA> mlist = new List<RMA>();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    mlist.Add(DataRowToHModel(ds.Tables[0].Rows[i]));
                }

                foreach (var item in mlist)
                {
                    StringBuilder sqlb = new StringBuilder();

                    sqlb.Append("Select  A.ID, DocLineNo, ItemInfo_ItemID, ItemInfo_ItemCode, ItemInfo_ItemName,  A1.SPECS AS ItemInfo_ItemSpecs, A.BillToSite_CustomerSite, A.PayerSite_CustomerSite, A.DescFlexField_PrivateDescSeg1, A2.QCDesc, A.ApplyQtyTU1, A.OrderPrice, A4.Name AS TU, A3.Name AS PU, A.SrcSoDoc, A.SrcSoDocNo, A.SrcDoc_EntityID, A.SrcDocLine_EntityID, A.SrcDocNo, A.ApplyMoneyTC ");
                    sqlb.Append(" FROM SM_RMALine A");
                    sqlb.Append(" inner join CBO_ItemMaster A1 ON A1.ID = A.ItemInfo_ItemID ");
                    sqlb.Append(" left join SM_RMALine_Trl A2 ON A2.ID = A.ID ");
                    sqlb.Append(" Left JOIN Base_UOM_Trl A3 ON A3.ID = A.PU ");
                    sqlb.Append(" LEFT JOIN Base_UOM_Trl A4 ON A4.ID = A.TU ");
                    sqlb.Append(" WHERE A.RMA = '" + item.ID + "' ");
                    //Pub.WriteLog.WriteLine(sql.ToString());

                    DataTable dtb = DbHelperSQL.QueryAsDT(sqlb.ToString(), U9constr);
                    if (dtb != null && dtb.Rows.Count > 0)
                    {
                        item.RMALines = new List<RMALine>();
                        for (int i = 0; i < dtb.Rows.Count; i++)
                        {
                            item.RMALines.Add(DataRowToBModel(dtb.Rows[i]));

                        }
                    }
                }
                result = mlist.ToJson();
            }
            else
            {
                throw new Exception("没有查到匹配的销售退货记录!");
                
            }
        }
         
    }
}
